﻿using Quartz;
using ServiceContainer.Commom;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Enums;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;
using YouWei.Utility;
using YouWei.Utility.Logging;

namespace ServiceContainer.Jobs
{
    public class WxMessageWarningJob:IJob
    {
        GcChatMessageService msgService = new GcChatMessageService();
        GcWarnWordService wordService = new GcWarnWordService();
        GcWxGroupOwnerService ownerService = new GcWxGroupOwnerService();
        GcWarnRuleService ruleService = new GcWarnRuleService();
        GcWarnSmsLogService smsLogService = new GcWarnSmsLogService();
        JobRecordService jobService = new JobRecordService();

        JobEnum jobEnum = JobEnum.WxMsgWarning;//作业ID

        public void Execute(IJobExecutionContext context)
        {
            var svJobRecord = new JobRecordService();
            var lastDataRecordTime = DateTime.Now.AddMinutes(-10);
            //获取最后一次执行作业的时间
            var oLastJobRecordInfo = svJobRecord.GetLastExcuteJobRecordInfo(jobEnum);
            if (oLastJobRecordInfo != null && !oLastJobRecordInfo.CreatedDate.IsNullDateTime())
            {
                lastDataRecordTime = oLastJobRecordInfo.CreatedDate;
            }
            else
            {
                oLastJobRecordInfo = new JobRecord();                
            }
            Dictionary<int, List<GcWarnWord>> dic = wordService.GetWarnWordGroup();
            List<GcWxGroupOwner> ownerList = ownerService.QueryAll();
            List<WxMessageInfo> list = msgService.GetListByTime(lastDataRecordTime);
            oLastJobRecordInfo.CreatedDate = DateTime.Now;
            int count = 0;
            foreach (var item in list)
            {
                try
                {
                    if (!string.IsNullOrWhiteSpace(item.Text))
                    {
                        CheckMessage(dic, item, ownerList, out count);
                    }
                  
                }
                catch (Exception ex)
                {
                    Log4netLog.Error(102, context.JobDetail.Key + "作业出现异常,异常信息为：" + ex);
                }
               
            }
            oLastJobRecordInfo.JobId = (int)jobEnum;
            oLastJobRecordInfo.LastDataRecordTime = DateTime.Now;
            oLastJobRecordInfo.SuccessCount = count;
            jobService.Insert(oLastJobRecordInfo);
        }

        private void CheckMessage(Dictionary<int, List<GcWarnWord>> dic,WxMessageInfo msgInfo, List<GcWxGroupOwner> ownerList,out int count)
        {
            count = 0;
            foreach (var item in dic)
            {
                bool isTrue = false;
                foreach (GcWarnWord wInfo in item.Value)
                {
                    if (Regex.IsMatch(msgInfo.Text, wInfo.Content))
                    {
                        isTrue = true;
                    }
                    else {
                        isTrue = false;
                        break;
                    }
                }
                if (isTrue)
                {
                    count++;
                    List<GcWxGroupOwner> oList = ownerList.Where(m => m.GroupID == m.GroupID && m.IsActive == 1).ToList();
                    if (oList.Count > 0)
                    {
                        SendSmsAndLog(oList[0].Mobile, item.Key, msgInfo);
                    }
                }
             
                
            }
        }


        private void SendSmsAndLog(string mobile,int ruleId, WxMessageInfo msgInfo)
        {
            string ruleName = ruleService.GetWarnRuleName(ruleId);
            string content = string.Format("{0}预警,{1},{2},{3}", ruleName, msgInfo.GroupName, msgInfo.UserNickName, msgInfo.Text);
            ChannelShortMsgHelper.SendProductMsg(mobile, content);
            GcWarnSmsLog logObj = new GcWarnSmsLog
            {
                CreatedTime = DateTime.Now,
                Mobile = mobile,
                RuleID = ruleId,
                SendContent = content
            };
            smsLogService.Insert(logObj);
        }
     
    }
}
